package com.reading.common.config;

import com.reading.common.constant.SequenceConstant;
import com.reading.common.persistence.view.SequenceMapper;
import com.reading.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
@Scope("singleton")
public class SequenceConfig {

    @Resource
    private SequenceMapper sequenceMapper;

    /**
     * 获取玩家Id
     */
    public synchronized Integer getUserId() {
        String seqName = SequenceConstant.KEY_USER_ID;
        return sequenceMapper.selectNext(seqName);
    }

    /**
     * 获取图书Id
     */
    public synchronized Integer getBookId() {
        String seqName = SequenceConstant.KEY_BOOK_ID;
        return sequenceMapper.selectNext(seqName);
    }

    /**
     * 获取图书新的章节号
     *
     * @param bookId
     * @return
     */
    public synchronized Integer getChapterNumber(Integer bookId) {
        String seqName = StringUtil.contentReplace(SequenceConstant.KEY_CHAPTER_NUMBER,
                SequenceConstant.PARAM_BOOK_ID, bookId);
        return sequenceMapper.selectNext(seqName);
    }

}
